package com.ccl.iot.template;import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ccl.iot.R;


public abstract class ActivityDialogTitled extends ActivityDialog {
	public static final String KEXTRA_NAME_TITLE = "Title";
	private RelativeLayout mTitleBar;

	public static void Show(Context aContext, Class<?> aClass, Object aTitle, DialogInterface aInterface){
		Bundle tBundle = null;
		
		if (aTitle != null){
			tBundle = ObtainBundle();
			
			tBundle.putString(KEXTRA_NAME_TITLE, GetString(aContext, aTitle));
		}
		
		Show(aContext, aClass, tBundle, aInterface);
	}
	
	public static void Show(Context aContext, Class<?> aClass, Object aTitle){
		Show(aContext, aClass, aTitle, null);
	}
	
	
	private String iTitleText = null;
	
	private View iBack = null;
	
	private TextView iTitle = null;
	
	private LinearLayout iContent = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
				
		initialize();
		
		initializeLayout();
	}
	
	private void initialize(){
		iTitleText = getString(KEXTRA_NAME_TITLE);
	}
	
	private void initializeLayout(){
		super.setContentView(R.layout.dialog_titled);
		
		iBack = super.findViewById(R.id.iBack);
		mTitleBar = (RelativeLayout) findViewById(R.id.iTitleBar);
		handleBackEvent();
		
		iTitle = (TextView) super.findViewById(R.id.iTitle);
		if (iTitle != null){
			iTitle.setText(iTitleText);
		}
		iContent = (LinearLayout) super.findViewById(R.id.iContentView);
	}

	protected void setTitleBarVisibilty(int visibilty){
		if(mTitleBar != null){
			mTitleBar.setVisibility(visibilty);
		}
	}
	
	public boolean setBackable(boolean aBackable){
		if (iBack != null){
			iBack.setVisibility(aBackable ? View.VISIBLE : View.GONE);
		}
		
		return false;
	}
	
	private void handleBackEvent(){
		if (iBack != null){
			iBack.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}
	}
	
	@Override
	public void setContentView(View view, LayoutParams params) {
		if (view != null && iContent != null){
			if (iContent.getChildCount() > 0){
				iContent.removeAllViews();
			}
			
			if (params != null){
				iContent.addView(view, params);
			}else{
				iContent.addView(view);
			}
		}
	}
	
	@Override
	public void setContentView(View view) {
		setContentView(view, null);
	}

	@Override
	public void setContentView(int layoutResID) {
		if (iContent != null){
			LayoutInflater.from(this).inflate(layoutResID, iContent);
		}
	}
	
	@Override
	public void setTitle(CharSequence title) {
		if (title != null){
			iTitleText = title.toString();
		}else{
			iTitleText = null;
		}
		
		if (iTitle != null){
			iTitle.setText(iTitleText);
		}
	}

	@Override
	public void setTitle(int titleId) {
		setTitle(GetString(this, titleId));
	}

	@Override
	public void setTitleColor(int textColor) {
		if (iTitle != null){
			iTitle.setTextColor(textColor);
		}
	}
	
	
}
